package com.viethp.mt.activities;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.viethp.mt.adapters.OApplicantAdapter;
import com.viethp.mt.common.CommonHelper;
import com.viethp.mt.common.GlobalVariables;
import com.viethp.mt.objects.GetApplicantListResponse;
import com.viethp.mt.objects.OApplicant;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.viethp.mt.activities.R;


public class AApplicantList extends Activity implements OnClickListener, OnItemClickListener{
	
	private static final String ITEM_ID = "item_id";
	private static final String XML_ITEM_ID = "item_id";
	private static final String XML_USER_ID = "user_id";
	private static final String XML_ACTIVITY_ID = "activity_id";
	private static final String XML_USERNAME = "username";
	private static final String XML_EMAIL = "email";
	private static final String XML_PHONE = "phone";
	private static final String XML_POSTING = "posting";
	private static final String XML_BOUGHT = "bought";
	private static final String XML_PAID = "paid";
	private static final String XML_BIDDING = "bidding";
	private static final String XML_STATUS= "status";
	private static final String XML_PRICE = "price";
	
	private ListView lv_applicants;
	private ArrayList<OApplicant> list_Oapplicants;
	private OApplicantAdapter listoapplicantadapter;
	private ProgressDialog progressDialog;
	
	public View footerView;
	private int totaPages;
	private int pages;
	private TextView txtshowmore;
//	private int countDemo, num;
	private Button btn_back;
	private String item_id;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_list_applicant);
		
		Intent extras = getIntent();
		item_id = extras.getStringExtra(ITEM_ID);
		Log.d("item_id", item_id);
		
		initComponents();
		setOnClickListener();
	}
	
	public void initComponents(){
		lv_applicants = (ListView) findViewById(R.id.lv_applicant);
		list_Oapplicants = new ArrayList<OApplicant>();
		
		//add
		footerView = ((LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.listfooter, null, false);
		lv_applicants.addFooterView(footerView);
		footerView.setVisibility(View.GONE);
		
		footerView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (pages < totaPages) {
					pages++;
					initData();

				}

			}
		});
		
		listoapplicantadapter = new OApplicantAdapter(this, R.layout.lv_applicants, list_Oapplicants);
		lv_applicants.setAdapter(listoapplicantadapter);
		lv_applicants.setOnItemClickListener(this);
		//add
		txtshowmore = (TextView) findViewById(R.id.idshow);
		
		progressDialog = new ProgressDialog(AApplicantList.this);
		progressDialog.setCancelable(false);
		
		totaPages = 1;
		pages = 1;
//		countDemo=1;
		
		btn_back = (Button) findViewById(R.id.btn_back);
		
		initData();
	}
	
	public void setOnClickListener(){
		btn_back.setOnClickListener(this);
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		Intent i = new Intent(AApplicantList.this,AApplicantDetails.class);
		i.putExtra(XML_ITEM_ID, String.valueOf(this.list_Oapplicants.get(position).getItemId()));
		i.putExtra(XML_USER_ID, String.valueOf(this.list_Oapplicants.get(position).getUserId()));
		i.putExtra(XML_ACTIVITY_ID, String.valueOf(this.list_Oapplicants.get(position).getActivityId()));
		i.putExtra(XML_USERNAME, String.valueOf(this.list_Oapplicants.get(position).getUsername()));
		i.putExtra(XML_EMAIL, String.valueOf(this.list_Oapplicants.get(position).getEmail()));
		i.putExtra(XML_PHONE, String.valueOf(this.list_Oapplicants.get(position).getPhone()));
		i.putExtra(XML_POSTING, String.valueOf(this.list_Oapplicants.get(position).getPosting()));
		i.putExtra(XML_BOUGHT, String.valueOf(this.list_Oapplicants.get(position).getBought()));
		i.putExtra(XML_PAID, String.valueOf(this.list_Oapplicants.get(position).getPaid()));
		i.putExtra(XML_BIDDING, String.valueOf(this.list_Oapplicants.get(position).getBidding()));
		i.putExtra(XML_STATUS, String.valueOf(this.list_Oapplicants.get(position).getStatic()));
		i.putExtra(XML_PRICE, String.valueOf(this.list_Oapplicants.get(position).getPrice()));
		startActivity(i);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			this.finish();
			break;

		default:
			break;
		}
		
	}
	
	private void startCallService() {
		
		progressDialog.setMessage(getString(R.string.pleasewait));
		progressDialog.show();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				Message message = new Message();
				message.obj = callService();
				handler.sendMessage(message);
				
			}
		}).start();
		
	}

	
	public void initData() {

		if (CommonHelper.isNetworkAvailable(this)) {
			startCallService();
		} else {
			CommonHelper.showWarning(this, getString(R.string.netstate));
		}

	}
 	
	private Handler handler = new Handler() {
			
			public void handleMessage(Message msg) {
				progressDialog.dismiss();
				
				if(msg.obj != null){
					GetApplicantListResponse response = (GetApplicantListResponse) msg.obj;
					if (response.getErrorCode() != null) {
						CommonHelper.showWarning(getParent(), response
								.getErrorCode().getErrMsg());
					} else {
						pages = response.getPage();
						Log.d("total",pages+"");
						totaPages = response.getTotalPage();
						Log.d("total",totaPages+"");
						ArrayList<OApplicant> OApplicantArr = response.getApplicantArray();
						if(OApplicantArr!=null){
							for(OApplicant oi : OApplicantArr){
								list_Oapplicants.add(oi);
							}
						}
						
						if (pages == totaPages) {

							if (lv_applicants.getFooterViewsCount() != 0)
								lv_applicants.removeFooterView(footerView);
							lv_applicants.setFooterDividersEnabled(true);

						} else {
							footerView.setVisibility(View.VISIBLE);
							lv_applicants.setFooterDividersEnabled(true);
							if (lv_applicants.getFooterViewsCount() == 0)
								lv_applicants.addFooterView(footerView);
						}

						txtshowmore.setText("Showing " + pages + " of " + totaPages
								+ " pages");
						listoapplicantadapter.notifyDataSetChanged();
						
					}
					
				}else {
					CommonHelper.showWarning(getParent(),
							getString(R.string.loaddatastate));
	
				}
	
			};
		};

	public GetApplicantListResponse callService(){
		SoapObject userRequest = new SoapObject(GlobalVariables.WS_NAMESPACE,
				GlobalVariables.WS_METHOD_GET_APPLICANT_LIST);
		userRequest.addProperty("item_id", item_id);
		userRequest.addProperty("page", pages);

		SoapObject request = new SoapObject(GlobalVariables.WS_NAMESPACE,
				GlobalVariables.WS_METHOD_GET_APPLICANT_LIST);
		request.addProperty(GlobalVariables.WS_METHOD_GET_APPLICANT_LIST, userRequest);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(
				GlobalVariables.WS_ENDPOINT,
				GlobalVariables.WS_CONNECTTION_TIME_OUT);
		SoapObject results = null;
		try {
			androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
					+ GlobalVariables.WS_METHOD_GET_APPLICANT_LIST, envelope);
		} catch (Exception e) {
			try {
				androidHttpTransport.call(GlobalVariables.WS_NAMESPACE
						+ GlobalVariables.WS_METHOD_GET_APPLICANT_LIST, envelope);
			} catch (Exception e1) {
				e1.printStackTrace();
				return null;

			}
		}
		try {
			results = (SoapObject) envelope.getResponse();
		} catch (SoapFault ex3) {
			ex3.printStackTrace();
			return null;
		}
		return new GetApplicantListResponse(results);
	}
}
